package com.it.day4;

/**
 * ClassName: FinalizeObj
 * Description: finalize()方法
 *
 * @Author: csx
 * @Create: 2025/4/28 - 8:50
 * @version: v1.0
 */
public class FinalizeObj {
    private static FinalizeObj obj;

    //只会调用一次
    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("调用了当前类重写的finalize()方法");
        obj = this;
    }

    public static void main(String[] args) {
        try {
            obj = new FinalizeObj();
            obj = null;
            System.out.println("第一次 GC");
            System.gc();
            Thread.sleep(2000);
            if (obj == null){
                System.out.println("obj 为空");
            } else {
                System.out.println("obj 不为空");
            }
            obj = null;
            System.out.println("第二次 GC");
            System.gc();
            Thread.sleep(2000);
            if (obj == null){
                System.out.println("obj 为空");
            } else {
                System.out.println("obj 不为空");
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
